﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;

namespace EPS.UI.Classes
{
    public static class TreeViewHelper
    {
        public static bool GetEnableRightClickSelection(DependencyObject obj)
        {
            return (bool)obj.GetValue(EnableRightClickSelectionProperty);
        }

        public static void SetEnableRightClickSelection(DependencyObject obj, bool value)
        {
            obj.SetValue(EnableRightClickSelectionProperty, value);
        }

        public static readonly DependencyProperty EnableRightClickSelectionProperty =
            DependencyProperty.RegisterAttached("EnableRightClickSelection", typeof(bool), typeof(TreeViewHelper), new UIPropertyMetadata(false, OnEnableRightClickSelectionChanged));

        private static void OnEnableRightClickSelectionChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            TreeView treeView = sender as TreeView;
            if (treeView != null)
            {
                if ((bool)e.NewValue)
                {
                    treeView.PreviewMouseRightButtonDown += new MouseButtonEventHandler(TreeView_PreviewMouseRightButtonDown);
                }
                else
                {
                    treeView.PreviewMouseRightButtonDown -= new MouseButtonEventHandler(TreeView_PreviewMouseRightButtonDown);
                }
            }
        }

        private static void TreeView_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            TreeView treeView = sender as TreeView;
            if (treeView != null)
            {
                SelectItemByRightClick(treeView);
            }
        }

        public static TreeViewItem SelectItemByRightClick(ItemsControl source)
        {
            /////////////////////////////////////////////////////////////////////////////
            //
            // Note: 对于TreeViewItem来说，如果被选中了，那肯定是它的父节点也被选中了
            //
            /////////////////////////////////////////////////////////////////////////////
            if (!(source is TreeView) && !(source is TreeViewItem))
            {
                throw new ArgumentException("只支持参数为TreeView或者TreeViewItem", "source");
            }

            foreach (object item in source.Items)
            {
                TreeViewItem currentItem = source.ItemContainerGenerator.ContainerFromItem(item) as TreeViewItem;
                Point mousePosition = Mouse.GetPosition(currentItem);

                Rect itemRect = VisualTreeHelper.GetDescendantBounds(currentItem);

                // 可能是选中的项，也可能是选中项的父节点
                if (itemRect.Contains(mousePosition))
                {
                    // 看看是不是它的孩子被选中了，否则就是它自己被选中了               
                    if (currentItem.IsExpanded)
                    {
                        // 只判断展开的项
                        TreeViewItem selectedItem = SelectItemByRightClick(currentItem);
                        if (selectedItem != null)
                        {
                            selectedItem.IsSelected = true;
                            return selectedItem;
                        }
                    }
                    currentItem.IsSelected = true;
                    return currentItem;
                }
            }
            return null;
        }
    }
}
